package comp
{
	use namespace comp_layout;
	
	import comp.style.css.CssAchieve;
	
	import flash.display.DisplayObject;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;

	/**
	 * 下拉列表控件
	 */
	public class DropDownList extends ListBase
	{
		include "style/SkinStyle" //皮肤属性
		
		public function DropDownList()
		{
			super();
			this._themeName="comp::DropDownList";
//			super.onItemBind=_onMyItemBind;
		}
		
		//*********** override *****************
		override protected function merge():void
		{
			var lay:String=getStyleDefault("layout","down");//getStyle("layout");
			var ml:Number=getStyle("marginLeft");
			var mr:Number=getStyle("marginRight");
			var mt:Number=getStyle("marginTop");
			var mb:Number=getStyle("marginBottom");
			
			
			var pl:Number=getStyle("paddingLeft");
			var pt:Number=getStyle("paddingTop");
			var pr:Number=getStyle("paddingRight");
			var pb:Number=getStyle("paddingBottom");
			
			var lx:Number=0,ly:Number=0;//上个对像x和上个对像y
			var pl1:Number=0,pt1:Number=0;//用来判断是否是第一个位置
			var i:uint=0;
			var child:DisplayObject;
			var chidth:Number=height-this.getStyle("marginBottom");//内容宽度
			
			if(lay.toLocaleLowerCase()=="down") //向下排列
			{	
				_titleButton.x=pl;
				_titleButton.y=pt;
				ly=_titleButton.y+_titleButton.height+mb;
				for(i=0;i<this.numChildren;i++)
				{
					child=this.getChildAt(i);
					if(child!=this._titleButton)// ||  !(child is  ScrollBar)
					{
						child.x=pl;
						child.y=ly+mt;	
						ly=child.y+child.height;
					}
				}
				
			}else
			{
				_titleButton.x=pl;
				_titleButton.y=-pb;
				ly=_titleButton.y-mt;
				for(i=0;i<this.numChildren;i++)
				{
					child=this.getChildAt(i);
					if(child!=this._titleButton)// || !(child is ScrollBar)
					{
						child.x=pl;//lx+ml+mr;
						child.y=ly-mt-child.height;	
						ly=child.y;
					}
				}//end for
			}
			CssAchieve.mergeTemplate(this);
			
			this.wholeRect=new Rectangle(0,0,this.width,this.height);

		}
		
		override	protected function createChildren():void
		{
			removeAllChild();
			
			var realW:Number=0;
			var realH:Number=0;
			if(!_autoW)
				realW=width-getStyle("paddingLeft")-getStyle("paddingRight");

			_titleButton=new Button();
			
			var c:String="";
			if(this.selectedIndex>=0)
				c=getValue(this.selectedValue);
			
			_titleButton.style=style;
			_titleButton.useTheme=this.useTheme;
			_titleButton.className=this.className;
			_titleButton.themeName=_themeName;
			_titleButton.text=c;
			_titleButton.clickFunction=checkOpen;
			_titleButton.selected=false;
			if(realW>0)
				_titleButton.width=realW;
			
			this.addChild(_titleButton);
				
			if(this.data==null)
				return;
			
			var itda:*;
			for(var i:uint=0;i<data.length;i++)
			{
				if(onItemBind==null)
					return;
				itda=data[i];
				
				var b:Button=new Button;		
				b.themeName=this.themeName+".Button";
				b.useTheme=this.useTheme;
				
				if(realW>0)
					b.width=realW;
				
				var sty:Object;
				if(this._preItemBind!=null)
					sty=_preItemBind(i,itda,b)
				
				if(sty!=null)
					b.style=sty;
				
				b.text=getValue(data[i]);
				
				if(_itemClassName)
					b.className=_itemClassName;
				
				b.clickFunction=itemClick;
				b.visible=false;
				b.name=getName(i);
				this.addChild(b);
					
					try{
						if(_onItemBinded!=null)
							_onItemBinded(i,itda,b);
					}catch(e:*){}
			}
		}
		
		//************ 内部方法 **************************
		
		private function getValue(item:*):String
		{
			if(item is String)
				return item;
			else if(item is int)
				return item+"";
			else if(showField)
				return item[this.showField].toString();
			else
				return "";
		}
		
		private function itemClick(e:MouseEvent):void
		{
			var b:Button=e.currentTarget as Button;
			var i:int=this.getIndexByName(b.name);

			this.selectedIndex=i;
						
			_titleButton.text=this.getValue(this.selectedValue);

			checkOpen(e);
			
			try{
				if(_clickFunction!=null)
					this._clickFunction(e);	
			}catch(e:Error)
			{
				trace(e);
			}
		}
		
		private function checkOpen(e:MouseEvent):void
		{
			var child:DisplayObject;

			if(_isOpen)
			{
				for(var i:int=0;i<this.numChildren;i++)
				{
					child=this.getChildAt(i);
					if(child!=this._titleButton)// ||  !(child is  ScrollBar)
						child.visible=false;
				}
				_isOpen=false;
			}else
			{
				for(var i:int=0;i<this.numChildren;i++)
				{
					child=this.getChildAt(i);
					if(child!=this._titleButton)// ||  !(child is  ScrollBar)
						child.visible=true;
				}
				_isOpen=true;
			}
			
			_titleButton.selected=_isOpen;

		}
		//********************* 样式 ***********************
		private var _itemClassName:String;//项的样式名
		
		private var _preItemBind:Function;//绑定之前 一般用于修改特定样式。
		private var _titleButton:Button;//标题按钮
		private var _isOpen:Boolean=false;//是否展开
		private var _clickFunction:Function;//选择tab按钮调用的function(this)

		//****************** 属性 *********************
//		
		/**
		 * 点击一项时调用
		 * @e 选择的项
		 * function(e:selectedItem):void;
		 */
		public function get clickFunction():Function
		{
			return _clickFunction;
		}
		
		public function set clickFunction(value:Function):void
		{
			_clickFunction = value;
		}
		/**
		 * up 向上
		 * down 向下
		 */
		public override function get layout():String
		{
			return _layout;
		}

		/**
		 * 项的样式名
		 */
		public function get itemClassName():String
		{
			return _itemClassName;
		}

		/**
		 * @private
		 */
		public function set itemClassName(value:String):void
		{
			_itemClassName = value;
		}
	
		public function get preItemBind():Function
		{
			return _preItemBind;
		}
		
		/**
		 * 当绑定的时候每项的样式返回syte
		 * 参数(索引,当前项的值，当前项显示对像)
		 * 格式function(i:int,data:*,item:Button):object样式 返回样式对象或者null
		 */
		public function set preItemBind(value:Function):void
		{
			_preItemBind = value;
		}

		/**
		 * 标题按钮
		 */
		public function get titleButton():Button
		{
			return _titleButton;
		}

	}
}